package com.ilovepostcard.controller;

import android.content.Context;

import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.Constants;
import com.ilovepostcard.utils.Utils;

public class UserController {
    
    /**
     * 用户登录后保存简单信息，然后后台抓取详细信息
     * @param context
     * @param user
     */
    public synchronized static void saveLoginUser(final Context context, final User user) {
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.LOGIN_STATUS, 1);
        Utils.setPreferenceLongData(context, Constants.PREFS_NAME, Constants.UID, user.UserID);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.USERNAME, user.Username);
        
        Utils.setPreferenceBooleanData(context, Constants.PREFS_NAME, Constants.HAVE_DETAIL_USER, false);
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                try {
                    PostcardResult result = ServerFactory.getServer().getUserInfo("", user.UserID);
                    User user = result.getUser();
                    saveDetailUser(context, user);
                } catch (Exception e) {

                }
            }
        }).start();
    }
    
    private synchronized static void removeLoginUser(final Context context) {
    	Utils.removePreference(context, Constants.PREFS_NAME, Constants.LOGIN_STATUS);
    	Utils.removePreference(context, Constants.PREFS_NAME, Constants.UID);
    	Utils.removePreference(context, Constants.PREFS_NAME, Constants.USERNAME);
    }
    
    public synchronized static void saveThirdPartySecret(final Context context, String type, String token, String secret) {
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, "third_party_type", type);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, "token", token);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, "secret", secret);
    }
    
    private synchronized static void removeThirdPartySecret(final Context context) {
    	Utils.removePreference(context, Constants.PREFS_NAME, "third_party_type");
    	Utils.removePreference(context, Constants.PREFS_NAME, "token");
    	Utils.removePreference(context, Constants.PREFS_NAME, "secret");
    }
    
    public synchronized static String getThirdPartyType(final Context context) {
        return Utils.getPreferenceData(context, Constants.PREFS_NAME, "third_party_type", "");
    }
    
    public synchronized static String[] getSinaAccessToken(final Context context) {
        return new String[] 
                {
                Utils.getPreferenceData(context, Constants.PREFS_NAME, "token", ""),
                Utils.getPreferenceData(context, Constants.PREFS_NAME, "secret", "")
                };
    }
    
    public synchronized static void logout(final Context context) {
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.LOGIN_STATUS, 0);
        Utils.setPreferenceBooleanData(context, Constants.PREFS_NAME, Constants.HAVE_DETAIL_USER, false);
        try {
        	removeLoginUser(context);
        } catch (Exception e) {

		}
        try {
        	removeThirdPartySecret(context);
        } catch (Exception e) {

		}
        try {
        	removeDetailUser(context);
        } catch (Exception e) {

		}
    }
    
    public synchronized static User getLoginUser(Context context) {
        int loginStatus = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.LOGIN_STATUS, 0);
        if (loginStatus == 0)
            return null;
        long uid = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.UID, 0L);
        String userName = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.USERNAME, "");
        User user = new User();
        user.UserID = uid;
        user.Username = userName;
        return user;
    }
    
    
    public synchronized static void saveDetailUser(Context context, User user) {
        if (user == null)
            return;
        Utils.setPreferenceLongData(context, Constants.PREFS_NAME, Constants.UID, user.UserID);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.USERNAME, user.Username);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.NICKNAME, user.Nickname);
        Utils.setPreferenceBooleanData(context, Constants.PREFS_NAME, Constants.IsValidEmail, user.IsValidEmail);
        Utils.setPreferenceBooleanData(context, Constants.PREFS_NAME, Constants.HasHeadImg, user.HasHeadImg);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.HeadImgUrl, user.HeadImgUrl);
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.Status, user.Status);
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.MyAttentionCount, user.MyAttentionCount);
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.BeAttentionCount, user.BeAttentionCount);
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.PublishCount, user.PublishCount);
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.PlatformType, user.PlatformType);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.Introduction, user.Introduction);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.MySpace, user.MySpace);
        Utils.setPreferenceIntData(context, Constants.PREFS_NAME, Constants.SEX, user.Sex);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.Birthday, user.Birthday);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.PROVINCE, user.Province);
        Utils.setPreferenceStringData(context, Constants.PREFS_NAME, Constants.CITY, user.City);
        Utils.setPreferenceBooleanData(context, Constants.PREFS_NAME, Constants.HAVE_DETAIL_USER, true);
    }
    
    private synchronized static void removeDetailUser(Context context) {
    	Utils.removePreference(context, Constants.PREFS_NAME, Constants.UID);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.USERNAME);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.NICKNAME);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.IsValidEmail);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.HasHeadImg);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.HeadImgUrl);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.Status);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.MyAttentionCount);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.BeAttentionCount);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.PublishCount);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.PlatformType);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.Introduction);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.MySpace);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.SEX);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.Birthday);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.PROVINCE);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.CITY);
        Utils.removePreference(context, Constants.PREFS_NAME, Constants.HAVE_DETAIL_USER);
    }
    
    public synchronized static User getDetailUser(Context context) {
        if (!Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.HAVE_DETAIL_USER, false)) {
            return null;
        }
        User user = new User();
        user.UserID = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.UID, 0L);
        user.Username = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.USERNAME, "");
        user.Nickname = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.NICKNAME, "");
        user.IsValidEmail = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.IsValidEmail, true);
        user.HasHeadImg = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.HasHeadImg, true);
        user.HeadImgUrl = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.HeadImgUrl, "");
        user.Status = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.Status, 0);
        user.MyAttentionCount = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.MyAttentionCount, 0);
        user.BeAttentionCount = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.BeAttentionCount, 0);
        user.PublishCount = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.PublishCount, 0);
        user.PlatformType = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.PlatformType, 0);
        user.Introduction = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.Introduction, "");
        user.MySpace = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.MySpace, "");
        user.Sex = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.SEX, 0);
        user.Birthday = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.Birthday, "");
        user.Province = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.PROVINCE, "");
        user.City = Utils.getPreferenceData(context, Constants.PREFS_NAME, Constants.CITY, "");
        return user;
    }
}
